package com.platform.service.impl;

import com.github.ltsopensource.core.commons.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.platform.dao.OrderDao;
import com.platform.dao.ShippingDao;
import com.platform.entity.OrderEntity;
import com.platform.entity.OrderGoodsEntity;
import com.platform.entity.ShippingEntity;
import com.platform.entity.UserEntity;
import com.platform.service.OrderGoodsService;
import com.platform.service.OrderService;
import com.platform.service.ProductService;
import com.platform.service.UserService;
import com.platform.utils.R;
import com.platform.utils.RRException;
import com.platform.utils.SmsUtil;
import com.platform.utils.StringUtils;
import org.apache.poi.util.StringUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private OrderGoodsService orderGoodsService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Override
    public OrderEntity queryObject(Integer id) {
        return orderDao.queryObject(id);
    }

    @Override
    public List<OrderEntity> queryList(Map<String, Object> map) {
        return orderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orderDao.queryTotal(map);
    }

    @Override
    public int save(OrderEntity order) {
        return orderDao.save(order);
    }

    @Override
    public int update(OrderEntity order) {
        return orderDao.update(order);
    }

    @Override
    public int delete(Integer id) {
        return orderDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return orderDao.deleteBatch(ids);
    }

    @Override
    public int confirm(Integer id) {
        OrderEntity orderEntity = queryObject(id);
        Integer shippingStatus = orderEntity.getShippingStatus();//发货状态
        Integer payStatus = orderEntity.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款，不能确认收货！");
        }
        if (4 == shippingStatus) {
            throw new RRException("此订单处于退货状态，不能确认收货！");
        }
        if (0 == shippingStatus) {
            throw new RRException("此订单未发货，不能确认收货！");
        }
        orderEntity.setShippingStatus(2);
        orderEntity.setOrderStatus(301);
        return orderDao.update(orderEntity);
    }

    @Override
    public int sendGoods(OrderEntity order) {
        Integer payStatus = order.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款！");
        }

        ShippingEntity shippingEntity = shippingDao.queryObject(order.getShippingId());
        if (null != shippingEntity) {
            order.setShippingName(shippingEntity.getName());
        }
        order.setOrderStatus(300);//订单已发货
        order.setShippingStatus(1);//已发货
        order.setShippingTime(new Date());

//        //发送短信
//        UserEntity userEntity = userService.queryObject(order.getUserId());
//        if (StringUtils.isNotEmpty(userEntity.getMobile())){
//            Map param = Maps.newHashMap();
//            param.put("orderId", order.getId());
//            List<OrderGoodsEntity> list = orderGoodsService.queryList(param);
//            List<String> goodsNames = list.stream().map(t->t.getGoodsName()).collect(Collectors.toList());
//            SmsUtil.deliver(userEntity.getMobile(), "您购买的" + StringUtil.join(",", goodsNames)+ "商品已经发货，请注意查收。");
//        }


        return orderDao.update(order);
    }


    @Override
    public R autoCancelOrderCallback(Integer orderId){
        OrderEntity orderEntity = queryObject(orderId);
        if (orderEntity.getOrderSn().equals(0)){
            //未付款，订单便跟为自动取消102，返还库存
            orderEntity.setOrderStatus(102);
            update(orderEntity);

            Map param = Maps.newHashMap();
            param.put("orderId", orderId);
            List<OrderGoodsEntity> orderGoods = orderGoodsService.queryList(param);
            orderGoods.forEach(t->productService.increaseGoodsNumber(t.getProductId(), t.getNumber()));
        }
        return R.ok();
    }

    @Override
    public BigDecimal getSubUsersOrderPrice(String subUsers) {
        return orderDao.getSubUsersOrderPrice(subUsers);
    }


    @Override
    public R cancelCoupon(Integer orderId) {
        int result = orderDao.cancelCoupon(orderId);
        if (result == 1){
            return R.ok();
        } else {
            return R.error();
        }
    }
}
